<?php
namespace Core;
use \GatewayWorker\Lib\Gateway;
class Threeyyy{
	public static function login($client_id,$message_data){
				if(!isset($message_data['room_id'])){
					throw new \Exception("\$message_data['room_id'] not set. client_ip:{$_SERVER['REMOTE_ADDR']} \$message:$message");
				}
				$room_id = $message_data['room_id'];
				$client_openid = $message_data['openid'];
				$client_avatar = $message_data['client_avatar'];
				$client_name = htmlspecialchars($message_data['client_name']);
				$client_3dscore = $message_data['score_3d'];
				$_SESSION['room_id'] = $room_id;
				$_SESSION['client_name'] = $client_name;
				$_SESSION['client_avatar'] = $client_avatar;
				$_SESSION['client_openid'] = $client_openid;
				$_SESSION['score_3d'] = $client_3dscore;
				Gateway::joinGroup($client_id, $room_id);
                return;
	}
	public static function shake($client_id,$message_data){
			$ttype = $message_data['ttype'];
				
				 switch($ttype){
					case 'gamestart':
						if(!isset($_SESSION['room_id'])){
							throw new \Exception("\$_SESSION['room_id'] not set. client_ip:{$_SERVER['REMOTE_ADDR']}");
						}
						
						if($message_data['openid']=='meepo_xinchang_yyy_3d'){
							$room_id = $_SESSION['room_id'];
							$new_message = array(
								'type'=>'3dyyy_start'
							);
							return Gateway::sendToGroup($room_id ,json_encode($new_message));
						}else{
							return false;
						}
					break; 
					case 'shake':
						if(!isset($_SESSION['room_id'])){
							throw new \Exception("\$_SESSION['room_id'] not set. client_ip:{$_SERVER['REMOTE_ADDR']}");
						}
						$_SESSION['score_3d'] = intval($message_data['score_3d']);
						break;
					case 'gameusers':
						if(!isset($_SESSION['room_id'])){
							throw new \Exception("\$_SESSION['room_id'] not set. client_ip:{$_SERVER['REMOTE_ADDR']}");
						}
						$room_id = $_SESSION['room_id'];
						$clients_list = Gateway::getClientSessionsByGroup($room_id);
						$new_message = array('type'=>'3dyyy_users','clients_list'=>$clients_list);
						return Gateway::sendToCurrentClient(json_encode($new_message));
						break;
				   case 'gamereset':
						if(!isset($_SESSION['room_id'])){
							throw new \Exception("\$_SESSION['room_id'] not set. client_ip:{$_SERVER['REMOTE_ADDR']}");
						}
						if($message_data['openid']=='meepo_xinchang_yyy_3d'){
							$room_id = $_SESSION['room_id'];
							$clients_list = Gateway::getClientSessionsByGroup($room_id);
							if(!empty($clients_list) && is_array($clients_list)){
									foreach($clients_list as $key=>$row){
									  if($row['score_3d']>0){
										$row['score_3d'] = 0;
										Gateway::updateSession($key,$row);
									  }
									}
							}
							$new_message = array('type'=>'3dyyy_gamerefresh');
							return Gateway::sendToGroup($room_id ,json_encode($new_message));
						}else{
							return false;
						}
				   case 'gameend':
					    if(!isset($_SESSION['room_id'])){
							throw new \Exception("\$_SESSION['room_id'] not set. client_ip:{$_SERVER['REMOTE_ADDR']}");
						}
						if($message_data['openid']=='meepo_xinchang_yyy_3d'){
							$room_id = $_SESSION['room_id'];
							$new_message = array('type'=>'3dyyy_gameend');
							return Gateway::sendToGroup($room_id ,json_encode($new_message));
						}else{
							return false;
						}
						break;
			   }
	}
}